package cn.anc.aonicardv.module.adpter.recorder;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;

import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;
import cn.anc.aonicardv.R;
import cn.anc.aonicardv.R2;
import cn.anc.aonicardv.module.ui.recorder.SettingSelectActivity;
import cn.anc.aonicardv.util.ui.CarSettingGuiHandler;
import cn.anc.httpcontrolutil.CarControl;
import cn.anc.httpcontrolutil.CarControlSettings;

/**
 * Created by yangdai on 2017/6/20.
 */

public class SettingSelectAdapter extends RecyclerView.Adapter {

    private List<String> settingOptionValues;
    private CarControlSettings.SettingItem currentSettingItem;
    private boolean isSingle;
    private SettingSelectActivity activity;

    public SettingSelectAdapter(CarControlSettings.SettingItem currentSettingItem, boolean isSingle, SettingSelectActivity activity) {
        this.currentSettingItem = currentSettingItem;
        this.settingOptionValues = currentSettingItem.settingItemOptions;
        this.isSingle = isSingle;
        this.activity = activity;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_setting_select, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        ViewHolder viewHolder = (ViewHolder) holder;
        final String option = settingOptionValues.get(position);
        viewHolder.optionValueTv.setText(option);
        if (!CarSettingGuiHandler.SETTING_ITEM_EV.equals(currentSettingItem.settingItemKey)) {
            CarControlSettings.SettingOptionGuiResource s = CarControlSettings.getSettingOptionGuiResourceByKey(currentSettingItem.settingItemKey, option);
            if (s != null)
                viewHolder.optionValueTv.setText(s.settingOptionDisplayStrId);
        } else {
            viewHolder.optionValueTv.setText(currentSettingItem.settingItemOptions.get(position));
        }
        if (position <settingOptionValues.size()&&position == settingOptionValues.size()-1) {
            viewHolder.mVItemLine.setVisibility(View.GONE);
        } else {
            viewHolder.mVItemLine.setVisibility(View.VISIBLE);
        }

        if (isSingle) {
            viewHolder.selectTv.setVisibility(View.VISIBLE);
            if (currentSettingItem.settingItemValue.equals(settingOptionValues.get(position))){
                viewHolder.selectTv.setBackgroundResource(R.mipmap.icon_selected);
            }
            else{
                viewHolder.selectTv.setBackgroundResource(R.mipmap.icon_unselect);
            }
            viewHolder.layout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (currentSettingItem.settingItemValue.equals(settingOptionValues.get(position))){
                        return;
                    }
                    activity.progressDialog.show();
                    if (null != settingOptionValues.get(position)) {
                        CarControl.doSetSettingItemValue(currentSettingItem.settingItemKey, settingOptionValues.get(position), new CarControl.OnCarControlCallback() {
                            @Override
                            public void OnCommandCallback(int code, CarControl.CommandResponseInfo info) {
                                if (info.rval == 0) {
                                    currentSettingItem.settingItemValue = settingOptionValues.get(position);
                                    activity.progressDialog.dismiss();
                                    activity.finish();
                                    notifyDataSetChanged();
                                } else {
                                    Toast.makeText(activity, activity.getString(R.string.my_error), Toast.LENGTH_SHORT).show();
                                    activity.progressDialog.dismiss();
                                    activity.finish();
                                }
                            }
                        });

                    } else {
                        Toast.makeText(activity, activity.getString(R.string.my_error), Toast.LENGTH_SHORT).show();
                        activity.progressDialog.dismiss();

                    }


                }
            });
        } else {
            viewHolder.switchButton.setVisibility(View.VISIBLE);
            viewHolder.selectTv.setVisibility(View.GONE);
            if (!TextUtils.isEmpty(currentSettingItem.settingItemValue)) {
                if (currentSettingItem.settingItemValue.contains(",")) {
                    String[] temp = currentSettingItem.settingItemValue.split(",");
                    for (int i = 0; i < temp.length; i++) {
                        if (option.equals(temp[i])) {
                            viewHolder.switchButton.setChecked(true);
                            break;
                        }
                    }
                } else {
                    if (option.equals(currentSettingItem.settingItemValue)) {
                        viewHolder.switchButton.setChecked(true);
                    }
                }

            }
            viewHolder.switchButton.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    activity.settingMultipleValue();

                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return settingOptionValues == null ? 0 : settingOptionValues.size();
    }


    public class ViewHolder extends RecyclerView.ViewHolder {

        @BindView(R2.id.tv_option_value)
        TextView optionValueTv;
        @BindView(R2.id.tv_select)
        TextView selectTv;
        @BindView(R2.id.layout)
        LinearLayout layout;
        @BindView(R2.id.switch_button)
        public CheckBox switchButton;
        @BindView(R2.id.v_item_line)
        View mVItemLine;
        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
